package  
{
  import com.bit101.components.HSlider;
  import com.bit101.components.PushButton;
  import flash.display.BitmapData;
  import flash.display.Shader;
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.geom.Point;
  import flash.display.StageScaleMode;
  import flash.display.StageAlign;
  import flash.geom.Rectangle;
  import mge.ScreenTransition;
  import mge.ui.ToolTipBox;
  
	/**
   * ...
   * @author ivan
   */
  public class ButtonMove extends Sprite
  {
    [Embed(source = '../res/logo.png')]
    private var rLogo:Class;
    
    [Embed(source = '../res/1.png')]
    private var r1:Class;
    [Embed(source = '../res/2.png')]
    private var r2:Class;
    [Embed(source = '../res/3.png')]
    private var r3:Class;
    [Embed(source = '../res/4.png')]
    private var r4:Class;
    
    
    private const NUM_BUTONS:int = 1;
    
    private var buttonsSprite:Sprite;
    
    public function ButtonMove() 
    {
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;
      
      addEventListener(Event.ENTER_FRAME, onEnterFrame);
      
      buttonsSprite = new Sprite();
      addChild(buttonsSprite);
      new PushButton(buttonsSprite, 0, 0, "Test!", createButtons);
      
      createButtons();
    }
    
    private function onEnterFrame(e:Event):void
    {
      for (var i:int = 0; i < NUM_BUTONS; i++)
        buttons[i].update();
    }
    
    private function createButtons(e:Event = null):void
    {
      for (var i:int = 0; i < buttons.length; i++)
        buttons[i].clear();
     
      buttons.splice(0, buttons.length);
      
      for (i = 0; i < NUM_BUTONS; i++)
      {
        var b:FuncyButton = new FuncyButton(i.toString(), new r1, new r2, new r3, new r4, buttonsSprite, new Point(Math.random() * 800, Math.random() * 400));
        b.addOnClickCallback(onButtonPress);
        addChild(new ToolTipBox(b.getSprite(), "Button Help"));
        buttons.push(b);
      }
    }
    
    private function onButtonPress(btnName:String):void
    {
      var x:Number = buttons[int(btnName)].x;
      var y:Number = buttons[int(btnName)].y;
      trace(x + ", " + y);
      var fadeSprite:Sprite = new Sprite();
      addChild(fadeSprite);
      var st:ScreenTransition = new ScreenTransition(fadeSprite, stage.width, stage.height);
      st.fadeToBlack(1000, null, false, ScreenTransition.EFFECT_SQUARES);
      
      //var bmp:BitmapData = new BitmapData(stage.width, stage.height, true, 0x000000);
      //bmp.noise(0);
      //bmp.fillRect(new Rectangle(0, 0, stage.width, stage.height), 0x000000);
      //bmp.fillRect(new Rectangle(10, 10, 10, 10), 0x000000);
      
      /*
      fadeSprite.graphics.beginBitmapFill(bmp);
      fadeSprite.graphics.drawRect(0, 0, stage.width, stage.height);
      fadeSprite.graphics.endFill();*/
    }
    
    private var buttons:Vector.<FuncyButton> = new Vector.<FuncyButton>();
  
  }
}

/*
import flash.display.Bitmap;
import flash.display.DisplayObjectContainer;
import flash.geom.Matrix;
import flash.geom.Point;
import mge.Ease;
import mge.Timer;
import mge.ui.Button;

internal class FuncyButton extends Button
{
  private const NUM_TIMERS:int = 5;
  
  public var speed:Number = 3000;
  public var speedVar:Number = 1500;
  
  public function FuncyButton(name:String, bmpEnabled:Bitmap, bmpDisabled:Bitmap, bmpSelected:Bitmap, bmpOver:Bitmap, parent:DisplayObjectContainer, orbitCenter:Point)
  {
    super(name, bmpEnabled, bmpDisabled, bmpSelected, bmpOver, parent);
    x = orbitCenter.x;
    y = orbitCenter.y;
    
    this.orbitCenter = orbitCenter;
    for (var i:int = 0; i < NUM_TIMERS * 3; i++)
    {
      var t:Timer = new Timer(speed + Math.random() * speedVar);
      t.start(true);
      t.setDelta(Math.random());
      if (i % 3 == 0) timersX.push(t);
      if (i % 3 == 1) timersY.push(t);
      if (i % 3 == 2) timersRot.push(t);
    }
  }
  
  public function update():void
  {
    var x_desp:Number = getAverage(timersX) * 6;
    var y_desp:Number = getAverage(timersY) * 6;
    var rot:Number = getAverage(timersRot) * 16;
    
    x = orbitCenter.x + x_desp;
    y = orbitCenter.y + y_desp;
    
    var m:Matrix = getSprite().transform.matrix;
    // m.c = getAverage(timersX) * 0.1;
    
    getSprite().transform.matrix = m;
    getSprite().rotation = rot;
  }
  
  private function getAverage(v:Vector.<Timer>):Number
  {
    var ans:Number = 0;
    for (var i:int = 0; i < NUM_TIMERS; i++)
      ans += v[i].getDelta(new Ease(Ease.sin2Pi_00));
    ans /= NUM_TIMERS;
    return ans;
  }
  
  public var timersX:Vector.<Timer> = new Vector.<Timer>();
  public var timersY:Vector.<Timer> = new Vector.<Timer>();
  public var timersRot:Vector.<Timer> = new Vector.<Timer>();
  private var orbitCenter:Point;
  
  
}*/